package com.jie.demo.base;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.Timeout;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * 测试超时
 */
public class BaseTime {

    @Test(timeout=1000)
    public void testWithTimeout() throws Exception{
      Thread.sleep(5000);
    }


    public static String log;
    private final CountDownLatch latch = new CountDownLatch(1);

    /**
     * 框架读取这个规则后，会查看每个测试方法是否满足规则
     */
    @Rule
    public Timeout globalTimeout = Timeout.seconds(10); // 10 seconds max per method tested

    @Test
    public void testSleepForTooLong() throws Exception {
        log += "ran1";
        TimeUnit.SECONDS.sleep(100); // sleep for 100 seconds
    }

    @Test
    public void testBlockForever() throws Exception {
        log += "ran2";
        latch.await(); // will block
    }
}
